var $$ = Dom7;
//初始化app
var myApp = new Framework7({
    modalTitle: '能量逗',
    modalButtonOk: '知道了',
    animateNavBackIcon: false
});
//初始化view 
var mainView = myApp.addView('.view-main', {
    dynamicNavbar: true
});
//模板
myApp.chickenTemplate = Template7.compile($$('script#chickenTemplate').html());
myApp.boxTemplate = Template7.compile($$('script#boxTemplate').html());
//开启loader
//myApp.showIndicator();
var chicken = [];
var box = [];
var boxTotal = [];

var sceneW = parseFloat($$('#scene').width());
var sceneH = parseFloat($$('#scene').height());

var initX = sceneW/2;
var initY = chickenSize/2;
var chickenCrashDis2 = Math.pow(chickenSize,2);
var boxOutCircleR = boxSize/Math.cos(Math.PI/4)/2;
var boxCrashDis = boxOutCircleR+chickenSize/2;
var boxCrashDis2 = Math.pow(boxCrashDis,2);

function addChicken(num,x,y,v,size){
    var deta = Math.random() * Math.PI;
    var vx0 = v * Math.cos(deta);
    var vy0 = v * Math.sin(deta);
    var chickenInit = {x:x,y:y,vx:vx0,vy:vy0};
    chicken.push(chickenInit);
    var chickenHtml = myApp.chickenTemplate({num:num,chickenSize:size,cleft:chickenSize/2,ctop:chickenSize/2});
    $$('#scene').append(chickenHtml);
    $$('.chicken'+num).transform('translate('+x+'px,'+y+'px)');
    $$('.chicken'+num).show();
}
addChicken(0,initX,initY,initV,chickenSize);

function addBox(num,x,y,size){
    var boxInit = {x:x,y:y};
    boxTotal.push(boxInit);
    var boxHtml = myApp.boxTemplate({num:num,boxSize:size});
    $$('#scene').append(boxHtml);
    //左上角顶点坐标
    var leftTopX = x-boxSize/2;
    var leftTopY = y-boxSize/2;
    $$('.box'+num).css({left:leftTopX+'px',top:leftTopY+'px'});
    $$('.box'+num).show();
    if(num>maxBoxNum-1){
        var delBoxNum = num-maxBoxNum;
        $$('.box'+delBoxNum).remove();
        var startBox = delBoxNum+1;
        var endBox = startBox+maxBoxNum;
        box = boxTotal.slice(startBox,endBox);
    }else{
        box = boxTotal;
    }
    //var t1 = box.length;
    //var t2 = boxTotal.length;
    //myApp.alert('当前box'+num+';box数组'+t1+';boxtotal数组'+t2);
}

var animId;
var lastTime = new Date().getTime();
function chickenRun() {
    var i,j,x,y,vx,vy,nowx,nowy;
    var now = new Date().getTime();
    var dif = (now - lastTime)/1000;
    for (i in chicken){
        x = chicken[i].x;
        y = chicken[i].y;
        vx = chicken[i].vx;
        vy = chicken[i].vy;
        nowx = x + vx * dif;
        nowy = y + vy * dif;

        for( j = parseInt(i)+1;j < chicken.length;j++){
            //console.log(j,chicken);
            var nowxj = (chicken[j].x) + (chicken[j].vx) * dif;
            var nowyj = (chicken[j].y) + (chicken[j].vy) * dif;
            var dis = Math.pow(nowx-nowxj,2)+Math.pow(nowy-nowyj,2);
            if(dis < chickenCrashDis2){
                vx = chicken[j].vx;
                vy = chicken[j].vy;
                chicken[j].vx = chicken[i].vx;
                chicken[j].vy = chicken[i].vy;
                chicken[i].vx = vx;
                chicken[i].vy = vy;
            }
        }
    }
    for (i in chicken){
        x = chicken[i].x;
        y = chicken[i].y;
        vx = chicken[i].vx;
        vy = chicken[i].vy;
        nowx = x + vx * dif;
        nowy = y + vy * dif;

        if(nowx < chickenSize/2 || nowx > sceneW-chickenSize/2){
            vx = -vx;
        }
        if(nowy < chickenSize/2 || nowy > sceneH-chickenSize/2){
            vy =  -vy;
        }

        nowx = x + vx * dif;
        nowy = y + vy * dif;

        for (j in box){
            dis = Math.pow(nowx-box[j].x,2)+Math.pow(nowy-box[j].y,2);
            if(dis < boxCrashDis2){
                vx = -vx;
                vy = -vy;
            }
        }

        x = x + vx * dif;
        y = y + vy * dif;
        $$('.chicken'+i).transform('translate('+x+'px,'+y+'px)');
        chicken[i].x = x;
        chicken[i].y = y;
        chicken[i].vx = vx;
        chicken[i].vy = vy;
    }
    lastTime = new Date().getTime();
    animId = $$.requestAnimationFrame(chickenRun);
}
animId = $$.requestAnimationFrame(chickenRun);


//初始化手势
var mc = new Hammer.Manager($$('#scene')[0], {
    recognizers: [
        // RecognizerClass, [options], [recognizeWith, ...], [requireFailure, ...]
        [Hammer.Rotate,{enable:false}],
        [Hammer.Swipe,{enable:false}],
        [Hammer.Press,{enable:false}],
        [Hammer.Tap,{enable:true}],
        [Hammer.Pan,{enable:false}],
        [Hammer.Pinch, { enable: false}]
    ]
});
var tapCecaleX = parseFloat($$('#scene').offset().left);
var tapCecaleY = parseFloat($$('#scene').offset().top);
var lastBoxNum = -1;
var maxBoxLeftTopX = sceneW - boxSize;
var maxBoxLeftTopY = sceneH - boxSize;
mc.on("tap",function(e){
    var tapX = parseFloat(e.center.x)-tapCecaleX;
    var tapY = parseFloat(e.center.y)-tapCecaleY;
    var boxLeftTopX = tapX - boxSize/2;
    var boxLeftTopY = tapY - boxSize/2;

    if(boxLeftTopX > 0 && boxLeftTopX < maxBoxLeftTopX && boxLeftTopY > 0 && boxLeftTopY < maxBoxLeftTopY){
        var canbeadded = true;
        var i;
        //不能与其他盒子有交集
        for (i in box){
            var dx = Math.abs(box[i].x-tapX);
            var dy = Math.abs(box[i].y-tapY);
            if(dx<boxSize && dy<boxSize){
                canbeadded = false;
                break;
            }
        }
        //不能与鸡有交集
        for (i in chicken){
            var d = Math.pow(tapX-chicken[i].x,2)+Math.pow(tapY-chicken[i].y,2);
            if (d<boxCrashDis2){
                canbeadded = false;
                break;
            }

        }
        if (canbeadded){
            var boxNum = lastBoxNum+1;
            lastBoxNum = boxNum;
            addBox(boxNum,tapX,tapY,boxSize);
        }
    }
});
$$('.addBall').click(function () {
    var num = $$('.chicken').length;
    addChicken(num,initX,initY,initV,chickenSize);
});

